﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;


namespace GaleSong
{
    /// <summary>
    ///     Denotes an instance which has a name.
    /// </summary>
    public interface IHaveName
    {
        string Name { get; set; }
    }

    /// <summary>
    ///     Denotes an instance which has a display name.
    /// </summary>
    public interface IHaveDisplayName
    {
        string DisplayName { get; }
    }

    /// <summary>
    ///     Denotes an instance which implements <see cref="IHaveName" /> and <see cref="IHaveDisplayName" />.
    /// </summary>
    public interface IDisplay : IHaveName, IHaveDisplayName
    {
    }

    /// <summary>
    ///     Denotes an extend <see cref="IDisplay" /> which is localization to update its DisplayName.
    /// </summary>
    public interface ILocalizableDisplay : IDisplay
    {
        bool AutoUpdate { get; set; }
        void UpdateDisplayName();
    }

    public class LocalizableDisplay : BindableBase, ILocalizableDisplay
    {
        protected string _displayName;
        protected string _name;
        private bool _autoUpdate;

        [LocalizableDisplayName(DisplayKey = "Property_Name")]
        [LocalizableCategory(DisplayKey = "Group_Common")]
        [Display(Order = 1)]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged(() => Name);
                UpdateDisplayName();
            }
        }

        [Browsable(false)]
        public string DisplayName
        {
            get { return _displayName; }
            set
            {
                _displayName = value;
                NotifyPropertyChanged(() => DisplayName);
            }
        }

        public void Handle(LanguageChangedMessage message)
        {
            UpdateDisplayName();
        }

        [Browsable(false)]
        public bool AutoUpdate
        {
            get { return _autoUpdate; }
            set
            {
                if (value != _autoUpdate)
                {
                    _autoUpdate = value;
                    if (value)
                    {
                        Messengers.ResourceMessenger.Register<LanguageChangedMessage>(this, UpdateDisplayName);
                    }
                    else
                    {
                        Messengers.ResourceMessenger.Unregister<LanguageChangedMessage>(this);
                    }
                }
            }
        }

        public virtual void UpdateDisplayName()
        {
            DisplayName = IoC.Get<IResourceService>().GetString(Name);
        }

        private void UpdateDisplayName(LanguageChangedMessage message)
        {
            UpdateDisplayName();
        }
    }

    public class LocalizableDisplayNameAttribute : DisplayNameAttribute
    {
        public string DisplayKey { get; set; }

        public override string DisplayName
        {
            get { return IoC.Get<IResourceService>().GetString(DisplayKey); }
        }
    }

    public class LocalizableCategoryAttribute : CategoryAttribute
    {
        public string DisplayKey { get; set; }

        protected override string GetLocalizedString(string value)
        {
            return IoC.Get<IResourceService>().GetString(DisplayKey);
        }
    }
}